home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 16 / CU Amiga Magazine's Super CD-ROM 16 (1997-10-16)(EMAP Images)(GB)[!][issue 1997-11].iso / CUCD / Graphics / Ghostscript / source / gdevpbm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-21  |  21.3 KB  |  718 lines

  1. /* Copyright (C) 1992, 1996, 1997 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* gdevpbm.c */
  20. /* Portable Bit/Gray/PixMap drivers */
  21. #include "gdevprn.h"
  22. #include "gscdefs.h"
  23. #include "gxlum.h"
  24.  
  25. /* Thanks are due to Jos Vos (jos@bull.nl) for an earlier P*M driver, */
  26. /* on which this one is based. */
  27.  
  28. /*
  29.  * There are 6 (pairs of) drivers here:
  30.  *    pbm[raw] - outputs PBM (black and white).
  31.  *    pgm[raw] - outputs PGM (gray-scale).
  32.  *    pgnm[raw] - outputs PBM if the page contains only black and white,
  33.  *      otherwise PGM.
  34.  *    ppm[raw] - outputs PPM (RGB).
  35.  *    pnm[raw] - outputs PBM if the page contains only black and white,
  36.  *      otherwise PGM if the page contains only gray shades,
  37.  *      otherwise PPM.
  38.  *    pkm[raw] - computes internally in CMYK, outputs PPM (RGB).
  39.  */
  40.  
  41. /*
  42.  * The code here is designed to work with variable depths for PGM and PPM.
  43.  * The code will work with any of the values in brackets, but the
  44.  * Ghostscript imager requires that depth be a power of 2 or be 24,
  45.  * so the actual allowed values are more limited.
  46.  *    pgm,pgnm: 1, 2, 4, 8, 16.  [1-16]
  47.  *    pgmraw,pgnmraw: 1, 2, 4, 8.  [1-8]
  48.  *    ppm,pnm: 4(3x1), 8(3x2), 16(3x5), 24(3x8), 32(3x10).  [3-32]
  49.  *    ppmraw,pnmraw: 4(3x1), 8(3x2), 16(3x5), 24(3x8).  [3-24]
  50.  *    pkm, pkmraw: 4(4x1), 8(4x2), 16(4x4), 32(4x8).  [4-32]
  51.  */
  52.  
  53. /* Structure for P*M devices, which extend the generic printer device. */
  54.  
  55. #define MAX_COMMENT 70            /* max user-supplied comment */
  56. struct gx_device_pbm_s {
  57.     gx_device_common;
  58.     gx_prn_device_common;
  59.     /* Additional state for P*M devices */
  60.     char magic;            /* n for "Pn" */
  61.     char comment[MAX_COMMENT + 1];    /* comment for head of file */
  62.     byte is_raw;            /* 1 if raw format, 0 if plain */
  63.     byte optimize;            /* 1 if optimization OK, 0 if not */
  64.     byte uses_color;        /* 0 if image is black and white, */
  65.                     /* 1 if gray (PGM or PPM only), */
  66.                     /* 2 or 3 if colored (PPM only) */
  67.     int alpha_text;            /* # of alpha bits for text (1,2,4) */
  68.     int alpha_graphics;        /* ditto for graphics (1,2,4) */
  69.     dev_proc_copy_alpha((*save_copy_alpha));
  70. };
  71. typedef struct gx_device_pbm_s gx_device_pbm;
  72.  
  73. #define bdev ((gx_device_pbm *)pdev)
  74.  
  75. /* ------ The device descriptors ------ */
  76.  
  77. /*
  78.  * Default X and Y resolution.
  79.  */
  80. #define X_DPI 72
  81. #define Y_DPI 72
  82.  
  83. /* Macro for generating P*M device descriptors. */
  84. #define pbm_prn_device(procs, dev_name, magic, is_raw, num_comp, depth, max_gray, max_rgb, optimize, print_page)\
  85. {    prn_device_body(gx_device_pbm, procs, dev_name,\
  86.       DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS, X_DPI, Y_DPI,\
  87.       0, 0, 0, 0,\
  88.       num_comp, depth, max_gray, max_rgb, max_gray + 1, max_rgb + 1,\
  89.       print_page),\
  90.     magic,\
  91.      { 0 },\
  92.     is_raw,\
  93.     optimize,\
  94.     0, 1, 1, 0\
  95. }
  96.  
  97. /* For all but PBM, we need our own color mapping and alpha procedures. */
  98. private dev_proc_map_rgb_color(pgm_map_rgb_color);
  99. private dev_proc_map_rgb_color(ppm_map_rgb_color);
  100. private dev_proc_map_color_rgb(pgm_map_color_rgb);
  101. private dev_proc_map_color_rgb(ppm_map_color_rgb);
  102. private dev_proc_map_cmyk_color(pkm_map_cmyk_color);
  103. private dev_proc_map_color_rgb(pkm_map_color_rgb);
  104. private dev_proc_put_params(ppm_put_params);
  105. private dev_proc_get_alpha_bits(ppm_get_alpha_bits);
  106. private dev_proc_copy_alpha(pnm_copy_alpha);
  107.  
  108. /* We need to initialize uses_color when opening the device, */
  109. /* and after each showpage. */
  110. private dev_proc_open_device(ppm_open);
  111. private dev_proc_output_page(ppm_output_page);
  112.  
  113. /* And of course we need our own print-page routines. */
  114. private dev_proc_print_page(pbm_print_page);
  115. private dev_proc_print_page(pgm_print_page);
  116. private dev_proc_print_page(ppm_print_page);
  117. private dev_proc_print_page(pkm_print_page);
  118.  
  119. /* The device procedures */
  120.  
  121. private gx_device_procs pbm_procs =
  122.     prn_procs(gdev_prn_open, ppm_output_page, gdev_prn_close);
  123.  
  124. /* See gdevprn.h for the template for the following. */
  125. #define pgpm_procs(p_map_rgb_color, p_map_color_rgb, p_map_cmyk_color) {\
  126.     ppm_open, NULL, NULL, ppm_output_page, gdev_prn_close,\
  127.     p_map_rgb_color, p_map_color_rgb, NULL, NULL, NULL, NULL, NULL, NULL,\
  128.     gdev_prn_get_params, ppm_put_params,\
  129.     p_map_cmyk_color, NULL, NULL, NULL, gx_page_device_get_page_device,\
  130.     ppm_get_alpha_bits\
  131. }
  132.  
  133. private gx_device_procs pgm_procs =
  134.   pgpm_procs(pgm_map_rgb_color, pgm_map_color_rgb, NULL);
  135. private gx_device_procs ppm_procs =
  136.   pgpm_procs(ppm_map_rgb_color, ppm_map_color_rgb, NULL);
  137. private gx_device_procs pkm_procs =
  138.   pgpm_procs(NULL, pkm_map_color_rgb, pkm_map_cmyk_color);
  139.  
  140. /* The device descriptors themselves */
  141. gx_device_pbm far_data gs_pbm_device =
  142.   pbm_prn_device(pbm_procs, "pbm", '1', 0, 1, 1, 1, 0, 0,
  143.          pbm_print_page);
  144. gx_device_pbm far_data gs_pbmraw_device =
  145.   pbm_prn_device(pbm_procs, "pbmraw", '4', 1, 1, 1, 1, 1, 0,
  146.          pbm_print_page);
  147. gx_device_pbm far_data gs_pgm_device =
  148.   pbm_prn_device(pgm_procs, "pgm", '2', 0, 1, 8, 255, 0, 0,
  149.          pgm_print_page);
  150. gx_device_pbm far_data gs_pgmraw_device =
  151.   pbm_prn_device(pgm_procs, "pgmraw", '5', 1, 1, 8, 255, 0, 0,
  152.          pgm_print_page);
  153. gx_device_pbm far_data gs_pgnm_device =
  154.   pbm_prn_device(pgm_procs, "pgnm", '2', 0, 1, 8, 255, 0, 1,
  155.          pgm_print_page);
  156. gx_device_pbm far_data gs_pgnmraw_device =
  157.   pbm_prn_device(pgm_procs, "pgnmraw", '5', 1, 1, 8, 255, 0, 1,
  158.          pgm_print_page);
  159. gx_device_pbm far_data gs_ppm_device =
  160.   pbm_prn_device(ppm_procs, "ppm", '3', 0, 3, 24, 255, 255, 0,
  161.          ppm_print_page);
  162. gx_device_pbm far_data gs_ppmraw_device =
  163.   pbm_prn_device(ppm_procs, "ppmraw", '6', 1, 3, 24, 255, 255, 0,
  164.          ppm_print_page);
  165. gx_device_pbm far_data gs_pnm_device =
  166.   pbm_prn_device(ppm_procs, "pnm", '3', 0, 3, 24, 255, 255, 1,
  167.          ppm_print_page);
  168. gx_device_pbm far_data gs_pnmraw_device =
  169.   pbm_prn_device(ppm_procs, "pnmraw", '6', 1, 3, 24, 255, 255, 1,
  170.          ppm_print_page);
  171. gx_device_pbm far_data gs_pkm_device =
  172.   pbm_prn_device(pkm_procs, "pkm", '3', 0, 4, 4, 1, 1, 0,
  173.          pkm_print_page);
  174. gx_device_pbm far_data gs_pkmraw_device =
  175.   pbm_prn_device(pkm_procs, "pkmraw", '6', 1, 4, 4, 1, 1, 0,
  176.          pkm_print_page);
  177.  
  178. /* ------ Initialization ------ */
  179.  
  180. /* Set the copy_alpha procedure if necessary. */
  181. private void
  182. ppm_set_copy_alpha(gx_device *pdev)
  183. {    if ( pdev->std_procs.copy_alpha != pnm_copy_alpha )
  184.       { bdev->save_copy_alpha = pdev->std_procs.copy_alpha;
  185.         if ( pdev->color_info.depth > 4 )
  186.           pdev->std_procs.copy_alpha = pnm_copy_alpha;
  187.       }
  188. }
  189.  
  190. private int
  191. ppm_open(gx_device *pdev)
  192. {    int code = gdev_prn_open(pdev);
  193.  
  194.     if ( code < 0 )
  195.       return code;
  196.     bdev->uses_color = 0;
  197.     ppm_set_copy_alpha(pdev);
  198.     return code;
  199. }
  200.  
  201. /* Print a page, and reset uses_color if this is a showpage. */
  202. private int
  203. ppm_output_page(gx_device *pdev, int num_copies, int flush)
  204. {    int code = gdev_prn_output_page(pdev, num_copies, flush);
  205.  
  206.     if ( code < 0 )
  207.       return code;
  208.     if ( flush )
  209.       bdev->uses_color = 0;
  210.     return code;
  211. }
  212.  
  213. /* ------ Color mapping routines ------ */
  214.  
  215. /* Map an RGB color to a PGM gray value. */
  216. /* Keep track of whether the image is black-and-white or gray. */
  217. private gx_color_index
  218. pgm_map_rgb_color(gx_device *pdev, ushort r, ushort g, ushort b)
  219. {    /* We round the value rather than truncating it. */
  220.     gx_color_value gray =
  221.         ((r * (ulong)lum_red_weight) +
  222.          (g * (ulong)lum_green_weight) +
  223.          (b * (ulong)lum_blue_weight) +
  224.          (lum_all_weights / 2)) / lum_all_weights
  225.         * pdev->color_info.max_gray / gx_max_color_value;
  226.     if ( !(gray == 0 || gray == pdev->color_info.max_gray) )
  227.       bdev->uses_color = 1;
  228.     return gray;
  229. }
  230.  
  231. /* Map a PGM gray value back to an RGB color. */
  232. private int
  233. pgm_map_color_rgb(gx_device *dev, gx_color_index color, ushort prgb[3])
  234. {    gx_color_value gray =
  235.       color * gx_max_color_value / dev->color_info.max_gray;
  236.     prgb[0] = gray;
  237.     prgb[1] = gray;
  238.     prgb[2] = gray;
  239.     return 0;
  240. }
  241.  
  242. /* Map an RGB color to a PPM color tuple. */
  243. /* Keep track of whether the image is black-and-white, gray, or colored. */
  244. private gx_color_index
  245. ppm_map_rgb_color(gx_device *pdev, ushort r, ushort g, ushort b)
  246. {    uint bitspercolor = pdev->color_info.depth / 3;
  247.     ulong max_value = pdev->color_info.max_color;
  248.     gx_color_value rc = r * max_value / gx_max_color_value;
  249.     gx_color_value gc = g * max_value / gx_max_color_value;
  250.     gx_color_value bc = b * max_value / gx_max_color_value;
  251.     if ( rc == gc && gc == bc )        /* black-and-white or gray */
  252.     {    if ( !(rc == 0 || rc == max_value) )
  253.           bdev->uses_color |= 1;        /* gray */
  254.     }
  255.     else                        /* color */
  256.       bdev->uses_color = 2;
  257.     return ((((ulong)rc << bitspercolor) + gc) << bitspercolor) + bc;
  258. }
  259.  
  260. /* Map a PPM color tuple back to an RGB color. */
  261. private int
  262. ppm_map_color_rgb(gx_device *dev, gx_color_index color, ushort prgb[3])
  263. {    uint bitspercolor = dev->color_info.depth / 3;
  264.     uint colormask = (1 << bitspercolor) - 1;
  265.     uint max_rgb = dev->color_info.max_color;
  266.  
  267.     prgb[0] = ((color >> (bitspercolor * 2)) & colormask) *
  268.       (ulong)gx_max_color_value / max_rgb;
  269.     prgb[1] = ((color >> bitspercolor) & colormask) *
  270.       (ulong)gx_max_color_value / max_rgb;
  271.     prgb[2] = (color & colormask) *
  272.       (ulong)gx_max_color_value / max_rgb;
  273.     return 0;
  274. }
  275.  
  276. /* Map a CMYK color to a pixel value. */
  277. private gx_color_index
  278. pkm_map_cmyk_color(gx_device *pdev, ushort c, ushort m, ushort y, ushort k)
  279. {    uint bitspercolor = pdev->color_info.depth >> 2;
  280.     ulong max_value = pdev->color_info.max_color;
  281.     uint cc = c * max_value / gx_max_color_value;
  282.     uint mc = m * max_value / gx_max_color_value;
  283.     uint yc = y * max_value / gx_max_color_value;
  284.     uint kc = k * max_value / gx_max_color_value;
  285.     gx_color_index color =
  286.       ((((((ulong)cc << bitspercolor) + mc) << bitspercolor) + yc)
  287.        << bitspercolor) + kc;
  288.  
  289.     return (color == gx_no_color_index ? color ^ 1 : color);
  290. }
  291.  
  292. /* Map a CMYK pixel value to RGB. */
  293. private int
  294. pkm_map_color_rgb(gx_device *dev, gx_color_index color, gx_color_value rgb[3])
  295. {    gx_color_index cshift = color;
  296.     int bpc = dev->color_info.depth >> 2;
  297.     uint mask = (1 << bpc) - 1;
  298.     uint max_value = dev->color_info.max_color;
  299.     uint c, m, y, k;
  300.  
  301.     k = cshift & mask;  cshift >>= bpc;
  302.     y = cshift & mask;  cshift >>= bpc;
  303.     m = cshift & mask;
  304.     c = cshift >> bpc;
  305. #define cvalue(c)\
  306.   ((gx_color_value)((ulong)(c) * gx_max_color_value / max_value))
  307.     /* We use our improved conversion rule.... */
  308.     rgb[0] = cvalue((max_value - c) * (max_value - k) / max_value);
  309.     rgb[1] = cvalue((max_value - m) * (max_value - k) / max_value);
  310.     rgb[2] = cvalue((max_value - y) * (max_value - k) / max_value);
  311. #undef cvalue
  312.     return 0;
  313. }
  314.  
  315. /* ------ Alpha capability ------ */
  316.  
  317. /* Put parameters. */
  318. private int
  319. ppm_put_alpha_param(gs_param_list *plist, gs_param_name param_name, int *pa,
  320.   bool alpha_ok)
  321. {    int code = param_read_int(plist, param_name, pa);
  322.     switch ( code )
  323.     {
  324.     case 0:
  325.         switch ( *pa )
  326.           {
  327.           case 1:
  328.             return 0;
  329.           case 2: case 4:
  330.             if ( alpha_ok )
  331.               return 0;
  332.           default:
  333.             code = gs_error_rangecheck;
  334.           }
  335.     default:
  336.         param_signal_error(plist, param_name, code);
  337.     case 1:
  338.         ;
  339.     }
  340.     return code;
  341. }
  342. private int
  343. ppm_put_params(gx_device *pdev, gs_param_list *plist)
  344. {    gx_device_color_info save_info;
  345.     int ncomps = pdev->color_info.num_components;
  346.     int bpc = pdev->color_info.depth / ncomps;
  347.     int ecode = 0;
  348.     int code;
  349.     int atext = bdev->alpha_text, agraphics = bdev->alpha_graphics;
  350.     bool alpha_ok;
  351.     long v;
  352.     const char _ds *vname;
  353.  
  354.     save_info = pdev->color_info;
  355.     if ( (code = param_read_long(plist, (vname = "GrayValues"), &v)) != 1 ||
  356.          (code = param_read_long(plist, (vname = "RedValues"), &v)) != 1 ||
  357.          (code = param_read_long(plist, (vname = "GreenValues"), &v)) != 1 ||
  358.          (code = param_read_long(plist, (vname = "BlueValues"), &v)) != 1
  359.        )
  360.       {    if ( code < 0 )
  361.           ecode = code;
  362.         else if ( v < 2 || v > (bdev->is_raw || ncomps > 1 ? 256 : 65536L) )
  363.           param_signal_error(plist, vname,
  364.                      ecode = gs_error_rangecheck);
  365.         else if ( v == 2 )
  366.           bpc = 1;
  367.         else if ( v <= 4 )
  368.           bpc = 2;
  369.         else if ( v <= 16 )
  370.           bpc = 4;
  371.         else if ( v <= 32 && ncomps == 3 )
  372.           bpc = 5;
  373.         else if ( v <= 256 )
  374.           bpc = 8;
  375.           else
  376.           bpc = 16;
  377.           if ( ecode >= 0 )
  378.           { static const byte depths[4][16] = {
  379.               { 1, 2, 0, 4, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 16 },
  380.               { 0 },
  381.               { 4, 8, 0, 16, 16, 0, 0, 24 },
  382.               { 4, 8, 0, 16, 0, 0, 0, 24 },
  383.             };
  384.             pdev->color_info.depth = depths[ncomps - 1][bpc - 1];
  385.             pdev->color_info.max_gray = pdev->color_info.max_color =
  386.               (pdev->color_info.dither_grays =
  387.                pdev->color_info.dither_colors = (int)v) - 1;
  388.           }
  389.       }
  390.     alpha_ok = bpc >= 5;
  391.     if ( (code = ppm_put_alpha_param(plist, "TextAlphaBits", &bdev->alpha_text, alpha_ok)) < 0 )
  392.       ecode = code;
  393.     if ( (code = ppm_put_alpha_param(plist, "GraphicsAlphaBits", &bdev->alpha_graphics, alpha_ok)) < 0 )
  394.       ecode = code;
  395.     if ( (code = ecode) < 0 ||
  396.          (code = gdev_prn_put_params(pdev, plist)) < 0
  397.        )
  398.       { bdev->alpha_text = atext;
  399.         bdev->alpha_graphics = agraphics;
  400.         pdev->color_info = save_info;
  401.       }
  402.     ppm_set_copy_alpha(pdev);
  403.     return code;
  404. }
  405.  
  406. /* Get the number of alpha bits. */
  407. private int
  408. ppm_get_alpha_bits(gx_device *pdev, graphics_object_type type)
  409. {    return (type == go_text ? bdev->alpha_text : bdev->alpha_graphics);
  410. }
  411.  
  412. /* Copy an alpha map, noting whether we may generate some non-black/white */
  413. /* colors through blending. */
  414. private int
  415. pnm_copy_alpha(gx_device *pdev, const byte *data, int data_x,
  416.   int raster, gx_bitmap_id id, int x, int y, int width, int height,
  417.   gx_color_index color, int depth)
  418. {    if ( pdev->color_info.depth < 24 ||
  419.          (color >> 8) == (color & 0xffff)
  420.        )
  421.       bdev->uses_color |= 1;
  422.     else
  423.       bdev->uses_color |= 2;
  424.     return (*bdev->save_copy_alpha)(pdev, data, data_x, raster, id,
  425.                     x, y, width, height, color, depth);
  426. }
  427.  
  428. /* ------ Internal routines ------ */
  429.  
  430. /* Print a page using a given row printing routine. */
  431. private int
  432. pbm_print_page_loop(gx_device_printer *pdev, char magic, FILE *pstream,
  433.   int (*row_proc)(P4(gx_device_printer *, byte *, int, FILE *)))
  434. {    uint raster = gdev_prn_raster(pdev);
  435.     byte *data = (byte *)gs_malloc(raster, 1, "pbm_begin_page");
  436.     int lnum = 0;
  437.     int code = 0;
  438.     if ( data == 0 )
  439.       return_error(gs_error_VMerror);
  440.     fprintf(pstream, "P%c\n", magic);
  441.     if ( bdev->comment[0] )
  442.       fprintf(pstream, "# %s\n", bdev->comment);
  443.     else
  444.       fprintf(pstream, "# Image generated by %s (device=%s)\n",
  445.           gs_product, pdev->dname);
  446.     fprintf(pstream, "%d %d\n", pdev->width, pdev->height);
  447.     switch ( magic )
  448.     {
  449.     case '1':        /* pbm */
  450.     case '4':        /* pbmraw */
  451.         break;
  452.     default:
  453.         fprintf(pstream, "%d\n", pdev->color_info.max_gray);
  454.     }
  455.     for ( ; lnum < pdev->height; lnum++ )
  456.     {    byte *row;
  457.         code = gdev_prn_get_bits(pdev, lnum, data, &row);
  458.         if ( code < 0 ) break;
  459.         code = (*row_proc)(pdev, row, pdev->color_info.depth, pstream);
  460.         if ( code < 0 ) break;
  461.     }
  462.     gs_free((char *)data, raster, 1, "pbm_print_page_loop");
  463.     return (code < 0 ? code : 0);
  464. }
  465.  
  466. /* ------ Individual page printing routines ------ */
  467.  
  468. /* Print a monobit page. */
  469. private int
  470. pbm_print_row(gx_device_printer *pdev, byte *data, int depth,
  471.   FILE *pstream)
  472. {    if ( bdev->is_raw )
  473.       fwrite(data, 1, (pdev->width + 7) >> 3, pstream);
  474.     else
  475.     {    byte *bp;
  476.         uint x, mask;
  477.         for ( bp = data, x = 0, mask = 0x80; x < pdev->width; )
  478.         {    putc((*bp & mask ? '1' : '0'), pstream);
  479.             if ( ++x == pdev->width || !(x & 63) )
  480.               putc('\n', pstream);
  481.             if ( (mask >>= 1) == 0 )
  482.               bp++, mask = 0x80;
  483.         }
  484.     }
  485.     return 0;
  486. }
  487. private int
  488. pbm_print_page(gx_device_printer *pdev, FILE *pstream)
  489. {    return pbm_print_page_loop(pdev, bdev->magic, pstream, pbm_print_row);
  490. }
  491.  
  492. /* Print a gray-mapped page. */
  493. private int
  494. pgm_print_row(gx_device_printer *pdev, byte *data, int depth,
  495.   FILE *pstream)
  496. {    /* Note that bpp <= 8 for raw format, bpp <= 16 for plain. */
  497.     uint mask = (1 << depth) - 1;
  498.     byte *bp;
  499.     uint x;
  500.     int shift;
  501.     if ( bdev->is_raw && depth == 8 )
  502.       fwrite(data, 1, pdev->width, pstream);
  503.     else
  504.       for ( bp = data, x = 0, shift = 8 - depth; x < pdev->width; )
  505.     {    uint pixel;
  506.         if ( shift < 0 )    /* bpp = 16 */
  507.            {    pixel = ((uint)*bp << 8) + bp[1];
  508.             bp += 2;
  509.            }
  510.         else
  511.            {    pixel = (*bp >> shift) & mask;
  512.             if ( (shift -= depth) < 0 )
  513.               bp++, shift += 8;
  514.            }
  515.         ++x;
  516.         if ( bdev->is_raw )
  517.           putc(pixel, pstream);
  518.         else
  519.           fprintf(pstream, "%d%c", pixel,
  520.               (x == pdev->width || !(x & 15) ? '\n' : ' '));
  521.     }
  522.     return 0;
  523. }
  524. private int
  525. pxm_pbm_print_row(gx_device_printer *pdev, byte *data, int depth,
  526.   FILE *pstream)
  527. {    /* Compress a PGM or PPM row to a PBM row. */
  528.     /* This doesn't have to be very fast. */
  529.     /* Note that we have to invert the data as well. */
  530.     int delta = (depth + 7) >> 3;
  531.     byte *src = data + delta - 1;        /* always big-endian */
  532.     byte *dest = data;
  533.     int x;
  534.     byte out_mask = 0x80;
  535.     byte out = 0;
  536.     if ( depth >= 8 )
  537.       {    /* One or more bytes per source pixel. */
  538.         for ( x = 0; x < pdev->width; x++, src += delta )
  539.           {    if ( !(*src & 1) )
  540.               out |= out_mask;
  541.             out_mask >>= 1;
  542.             if ( !out_mask )
  543.               out_mask = 0x80,
  544.               *dest++ = out,
  545.               out = 0;
  546.           }
  547.       }
  548.     else
  549.       {    /* Multiple source pixels per byte. */
  550.         byte in_mask = 0x100 >> depth;
  551.         for ( x = 0; x < pdev->width; x++ )
  552.           {    if ( !(*src & in_mask) )
  553.               out |= out_mask;
  554.             in_mask >>= depth;
  555.             if ( !in_mask )
  556.               in_mask = 0x100 >> depth,
  557.               src++;
  558.             out_mask >>= 1;
  559.             if ( !out_mask )
  560.               out_mask = 0x80,
  561.               *dest++ = out,
  562.               out = 0;
  563.           }
  564.     }
  565.     if ( out_mask != 0x80 )
  566.       *dest = out;
  567.     return pbm_print_row(pdev, data, 1, pstream);
  568. }
  569. private int
  570. pgm_print_page(gx_device_printer *pdev, FILE *pstream)
  571. {    return (bdev->uses_color == 0 && bdev->optimize ?
  572.         pbm_print_page_loop(pdev, bdev->magic - 1, pstream,
  573.                     pxm_pbm_print_row) :
  574.         pbm_print_page_loop(pdev, bdev->magic, pstream,
  575.                     pgm_print_row) );
  576. }
  577.  
  578. /* Print a color-mapped page. */
  579. private int
  580. ppgm_print_row(gx_device_printer *pdev, byte *data, int depth,
  581.   FILE *pstream, bool color)
  582. {    /* If color=false, write only one value per pixel; */
  583.     /* if color=true, write 3 values per pixel. */
  584.     /* Note that depth <= 24 for raw format, depth <= 32 for plain. */
  585.     uint bpe = depth / 3;    /* bits per r/g/b element */
  586.     uint mask = (1 << bpe) - 1;
  587.     byte *bp;
  588.     uint x;
  589.     uint eol_mask = (color ? 7 : 15);
  590.     int shift;
  591.     if ( bdev->is_raw && depth == 24 && color )
  592.       fwrite(data, 1, pdev->width * (depth / 8), pstream);
  593.     else
  594.       for ( bp = data, x = 0, shift = 8 - depth; x < pdev->width; )
  595.     {    bits32 pixel = 0;
  596.         uint r, g, b;
  597.         switch ( depth >> 3 )
  598.            {
  599.         case 4:
  600.             pixel = (bits32)*bp << 24; bp++;
  601.             /* falls through */
  602.         case 3:
  603.             pixel += (bits32)*bp << 16; bp++;
  604.             /* falls through */
  605.         case 2:
  606.             pixel += (uint)*bp << 8; bp++;
  607.             /* falls through */
  608.         case 1:
  609.             pixel += *bp; bp++;
  610.             break;
  611.         case 0:            /* bpp == 4, bpe == 1 */
  612.             pixel = *bp >> shift;
  613.             if ( (shift -= depth) < 0 )
  614.               bp++, shift += 8;
  615.             break;
  616.            }
  617.         ++x;
  618.         b = pixel & mask;  pixel >>= bpe;
  619.         g = pixel & mask;  pixel >>= bpe;
  620.         r = pixel & mask;
  621.         if ( bdev->is_raw )
  622.         {    if ( color )
  623.             {    putc(r, pstream);
  624.                 putc(g, pstream);
  625.             }
  626.             putc(b, pstream);
  627.         }
  628.         else
  629.         {    if ( color )
  630.               fprintf(pstream, "%d %d ", r, g);
  631.             fprintf(pstream, "%d%c", b,
  632.                 (x == pdev->width || !(x & eol_mask) ?
  633.                  '\n' : ' '));
  634.         }
  635.     }
  636.     return 0;
  637. }
  638. private int
  639. ppm_print_row(gx_device_printer *pdev, byte *data, int depth,
  640.   FILE *pstream)
  641. {    return ppgm_print_row(pdev, data, depth, pstream, true);
  642. }
  643. private int
  644. ppm_pgm_print_row(gx_device_printer *pdev, byte *data, int depth,
  645.   FILE *pstream)
  646. {    return ppgm_print_row(pdev, data, depth, pstream, false);
  647. }
  648. private int
  649. ppm_print_page(gx_device_printer *pdev, FILE *pstream)
  650. {    return (bdev->uses_color >= 2 || !bdev->optimize ?
  651.         pbm_print_page_loop(pdev, bdev->magic, pstream,
  652.                     ppm_print_row) :
  653.         bdev->uses_color == 1 ?
  654.         pbm_print_page_loop(pdev, bdev->magic - 1, pstream,
  655.                     ppm_pgm_print_row) :
  656.         pbm_print_page_loop(pdev, bdev->magic - 2, pstream,
  657.                     pxm_pbm_print_row) );
  658. }
  659.  
  660. /* Print a faux CMYK page. */
  661. private int
  662. pkm_print_row(gx_device_printer *pdev, byte *data, int depth,
  663.   FILE *pstream)
  664. {    byte *bp;
  665.     uint x;
  666.     int shift;
  667.     ulong max_value = pdev->color_info.max_color;
  668.     uint mask = (depth >= 8 ? 0xff : (1 << depth) - 1);
  669.  
  670.     for ( bp = data, x = 0, shift = 8 - depth; x < pdev->width; )
  671.     {    bits32 pixel = 0;
  672.         gx_color_value rgb[3];
  673.         uint r, g, b;
  674.  
  675.         switch ( depth >> 3 )
  676.            {
  677.         case 4:
  678.             pixel = (bits32)*bp << 24; bp++;
  679.             /* falls through */
  680.         case 3:
  681.             pixel += (bits32)*bp << 16; bp++;
  682.             /* falls through */
  683.         case 2:
  684.             pixel += (uint)*bp << 8; bp++;
  685.             /* falls through */
  686.         case 1:
  687.             pixel += *bp; bp++;
  688.             break;
  689.         case 0:            /* bpp == 4 */
  690.             pixel = (*bp >> shift) & mask;
  691.             if ( (shift -= depth) < 0 )
  692.               bp++, shift += 8;
  693.             break;
  694.            }
  695.         ++x;
  696.         pkm_map_color_rgb((gx_device *)pdev, pixel, rgb);
  697.         r = rgb[0] * max_value / gx_max_color_value;
  698.         g = rgb[1] * max_value / gx_max_color_value;
  699.         b = rgb[2] * max_value / gx_max_color_value;
  700.         if ( bdev->is_raw )
  701.         {    putc(r, pstream);
  702.             putc(g, pstream);
  703.             putc(b, pstream);
  704.         }
  705.         else
  706.         {    fprintf(pstream, "%d %d %d%c", r, g, b,
  707.                 (x == pdev->width || !(x & 7) ?
  708.                  '\n' : ' '));
  709.         }
  710.     }
  711.     return 0;
  712. }
  713. private int
  714. pkm_print_page(gx_device_printer *pdev, FILE *pstream)
  715. {    return pbm_print_page_loop(pdev, bdev->magic, pstream,
  716.                    pkm_print_row);
  717. }
  718.